/*******************************************************
  > Copyright (C) 2024 ==IOT== All rights reserved.
  > File Name: 7.c
  > Author: wyf
  > Website:www.lv1.org
  > Created Time: 2024年08月04日 星期日 19时37分08秒
 ***************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <assert.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>

int main(int argc, const char* argv[])
{
	int fd[2] = {0};
	int ft[2] = {0};
	char src[100] = {"达咩"};
	char dest[100] = {"妙静"};
	pid_t pid;
	int res = 0;


	res = pipe(fd);
	if(res == -1 ){
		perror("pipe");
	}
	res = pipe(ft);
	if(res == -1){
		perror("pipe");
	}

	pid = fork();//fork必须是在管道之后，不然会有多个文件描述符

	//子
	if(pid == 0){
		close(fd[1]);
		int ret = 0;
		ret = read(fd[0],src,sizeof(src));
		if(ret == -1 ){
			perror("read");
		}
		printf("%s\n",src);
		close(fd[0]);
		//管道2
		close(ft[0]);
		ret = write(ft[1],dest,strlen(dest));
		if(ret == -1 ){
			perror("write");
		}
		close(ft[1]);
		exit(0);	
	}


	//父
	close(fd[0]);
	res = write(fd[1],src,strlen(src));
	if(res == -1 ){
		perror("write");
	}
	close(fd[1]);


	wait(NULL);
	//管道2
	close(ft[1]);
	res = read(ft[0],dest,sizeof(dest));
	if(res == -1 ){
		perror("read");
	}
	printf("%s\n",dest);
	close(ft[0]);


	return 0;
}
